package cn.wolfcode.service.impl;

import cn.wolfcode.common.domain.UserInfo;
import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.domain.Product;
import cn.wolfcode.domain.SeckillProduct;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.feign.ProductFeignApi;
import cn.wolfcode.mapper.OrderInfoMapper;
import cn.wolfcode.mapper.SeckillProductMapper;
import cn.wolfcode.mq.OrderMQResult;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.ISeckillProductService;
import cn.wolfcode.util.DateUtil;
import cn.wolfcode.util.IdGenerateUtil;
import cn.wolfcode.util.UserUtil;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Slf4j
@Service
public class SeckillProductServiceImpl implements ISeckillProductService {
    @Autowired
    private SeckillProductMapper seckillProductMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private ProductFeignApi productFeignApi;

    @Autowired
    private OrderInfoMapper orderInfoMapper;


    @Override
    public List<SeckillProductVo> selectByTime(int time) {
        List<SeckillProductVo> vos=new ArrayList<>();
        List<SeckillProduct> seckillProducts = seckillProductMapper.queryCurrentlySeckillProduct(time);
        List<Long> ids = new ArrayList<>();
        if (seckillProducts != null && seckillProducts.size() > 0) {
            for (SeckillProduct seckillProduct : seckillProducts) {
                ids.add(seckillProduct.getProductId());
            }
        }
        Result<List<Product>> results = productFeignApi.getIds(ids);
        Map<Long, Product> map = new HashMap<>();
        if(results !=null && !results.hasError()){
            List<Product> products = results.getData();
            if(products != null&&products.size()>0){
                for (Product product : products) {
                    map.put(product.getId(),product);
                }
            }
        }
        for (SeckillProduct seckillProduct : seckillProducts) {
            SeckillProductVo seckillProductVo = new SeckillProductVo();
            Product product = map.get(seckillProduct.getProductId());
            BeanUtils.copyProperties(product,seckillProductVo);
            BeanUtils.copyProperties(seckillProduct,seckillProductVo);
            vos.add(seckillProductVo);
        }
        return vos;
    }

    @Override
    public List<SeckillProductVo> queryByTime(Integer time) {
        List<SeckillProductVo> vos=new ArrayList<>();
        List<Object> values = redisTemplate.opsForHash().values(SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time.toString()));
        for (Object value : values) {
            vos.add(JSON.parseObject(value.toString(), SeckillProductVo.class));
        }
        return vos;
    }

    @Override
    public SeckillProductVo find(Integer time, Long seckillId) {
        Object o = redisTemplate.opsForHash().get(SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time.toString()), seckillId + "");
        SeckillProductVo seckillProductVo = JSON.parseObject(o.toString(), SeckillProductVo.class);
        return seckillProductVo;

    }

    @Override
    public void syncRedisStockCount(OrderMQResult orderMQResult) {
        Long seckillId = orderMQResult.getSeckillId();
        Integer time = orderMQResult.getTime();

        int stockCount = seckillProductMapper.getStockCount(seckillId);
        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_REAL_COUNT_HASH.getRealKey(time.toString()), seckillId+"", stockCount+"");
        log.info("修改成功真正库存");
    }
}
